package com.bootdo.clouddo.controller.douban;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.clouddo.utils.R;

@RestController
@RequestMapping("/movie/douban")
public class MovieController {
	
	public static final String host = "https://douban.uieee.com";
	
	public static final String host2 = "https://api.douban.com";
	
	public static final String list_pre = "/v2/movie/";
	
	@GetMapping("/list/{type}")
	public R list(@PathVariable String type, int start, int count) {
		try {
			JSONObject resultVo = sendGetRequest(list_pre + type + "?start="+start+"&count="+count);
			if(resultVo != null) {
				return R.data(resultVo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("/subject/{id}")
	public R subject(@PathVariable String id) {
		try {
			JSONObject resultVo = sendGetRequest(list_pre + "subject/" + id);
			if(resultVo != null) {
				return R.data(resultVo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
	/**
	 * 影评
	 * @param id
	 * @return
	 */
	@GetMapping("/review/{id}")
	public R review(@PathVariable String id) {
		try {
			JSONObject resultVo = sendGetRequest(list_pre + "review/" + id);
			if(resultVo != null) {
				return R.data(resultVo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
	/**
	 * 影人
	 * @param id
	 * @return
	 */
	@GetMapping("/celebrity/{id}")
	public R celebrity(@PathVariable String id) {
		try {
			JSONObject resultVo = sendGetRequest(list_pre + "celebrity/" + id);
			if(resultVo != null) {
				return R.data(resultVo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
	/**
	 * 搜索
	 * @param q
	 * @return
	 */
	@GetMapping("/search")
	public R search(String q) {
		try {
			JSONObject resultVo = sendGetRequest(list_pre + "search?q=" + q);
			if(resultVo != null) {
				return R.data(resultVo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
	
	
	
	public static JSONObject sendGetRequest(String url) {
		return sendGetRequest(url, new HashMap<>());
	}
	
	public static JSONObject sendGetRequest(String url, Map<String, String> params) {
		return sendGetRequest(url, params, null);
	}
	
	public static JSONObject sendGetRequest(String url, Map<String, String> params, String token){
        RestTemplate client = new RestTemplate();
        HttpMethod method = HttpMethod.GET;
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(params);
        
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<JSONObject> response = client.exchange(host + url, method, requestEntity, JSONObject.class);

        if(response == null || response.getBody() == null) {
        	response = client.exchange(host2 + url, method, requestEntity, JSONObject.class);
        }
        return response.getBody();
    }
	

}
